home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 299_01 / conver.c < prev    next >
C/C++ Source or Header  |  1989-12-28  |  13KB  |  272 lines

  1. /*
  2. ---------------------------------------------------------------------------
  3. filename: conver.c
  4. author: g. m. crews
  5. creation date: 28-Jul-1988
  6. date of last revision: 28-Jul-1989
  7.  
  8. this function calculates and returns the conversion factor required for
  9. going from units represented by an input string of characters, to units
  10. represented by an output string of characters.
  11.    
  12. to choose the proper strings for various units, see the conversion table
  13. array of structures defined below.  this table gives each unit's key,
  14. conversion factor, string, and base unit's key.  the conversion factor is
  15. the number by which to multiply in order to convert to the given base unit.
  16.    
  17. notice that all compatible units have the same base key.  thus, to
  18. convert from units given by an input_key to units given by an output
  19. key, the function simply returns the input conversion factor divided by
  20. the output conversion factor.
  21.    
  22. if the input_key or output_key are not found in the conversion_table,
  23. or the keys do not have the same base, 0. is returned.
  24.  
  25. conversion factors were obtained from ASTM E380-82 Standard for Metric
  26. Practice.
  27.  
  28. example, to go from feet to inches:
  29.  
  30.     conversion_factor = calc_conversion_factor("ft", "in");
  31. ---------------------------------------------------------------------------
  32. */
  33. #include <string.h>
  34.  
  35. double calc_conversion_factor(char input_str[],char output_str[]);
  36.  
  37. #define NUM_CFACTORS_IN_TBL 160
  38. #define MAX_LEN_UNITS 14
  39.  
  40. double calc_conversion_factor(
  41. char input_str[],  /* string describing input units (see table below) */
  42. char output_str[]) /* string describing output units */
  43. {
  44.     int i,j;
  45.     double factor1, factor2;
  46.     int base1, base2;
  47.  
  48.     static struct conversion_factors {
  49.     int    key;         /* code number for this units */
  50.     double factor;         /* multiplier to obtain base (reference) units */
  51.     int    base;         /* code number for base units */
  52.     char   units[MAX_LEN_UNITS+1];
  53.                  /* string describing units */
  54.     } conversion_table[NUM_CFACTORS_IN_TBL] = {
  55.  
  56.     /* This is the array containing conversion factors.  Note that an
  57.        askerisk (*) in the comment describing a unit means the conversion
  58.        factor is exact. */
  59.  
  60.     /* LENGTH: */
  61.     {  100, 1.000000E+00,  100, "m"        }, /* *(meter) */
  62.     {  101, 1.000000E-10,  100, "angstrom"    },
  63.     {  102, 1.495979E+11,  100, "ast-unit"    }, /* astronomical unit */
  64.     {  103, 2.011684E+01,  100, "chain"     }, /* US survey ft */
  65.     {  104, 1.828804E+00,  100, "fathom"    }, /* US survey ft */
  66.     {  105, 1.000000E-15,  100, "fermi"     },
  67.     {  106, 3.048000E-01,  100, "ft"        }, /* *(foot) */
  68.     {  107, 3.048006E-01,  100, "survey-ft"    }, /* US survey ft */
  69.     {  108, 2.540000E-02,  100, "in"        }, /* *(inch) */
  70.     {  109, 9.460550E+15,  100, "light-year"    },
  71.     {  110, 2.540000E-08,  100, "micro-in"    }, /* *microinch */
  72.     {  111, 1.000000E-06,  100, "micron"    }, /* *micron */
  73.     {  112, 2.540000E-05,  100, "mil"        }, /* *mil */
  74.     {  113, 1.852000E+03,  100, "naut-mi"    }, /* nautical mile */
  75.     {  114, 1.609347E+03,  100, "mi"        }, /* statute mile
  76.                               (US survey foot) */
  77.     {  115, 3.085678E+16,  100, "parsec"    },
  78.     {  116, 4.217518E-03,  100, "pica"        }, /* printer's */
  79.     {  117, 3.514598E-04,  100, "point"     }, /* *(printer's) */
  80.     {  118, 5.029210E+00,  100, "rod"        }, /* rod (US survey foot) */
  81.     {  119, 9.144000E-01,  100, "yd"        }, /* *yard */
  82.     /* ACCELERATION: */
  83.     {  200, 1.000000E+00,  200, "m/s2"        }, /* meter/sec2 */
  84.     {  201, 3.048000E-01,  200, "ft/s2"     },
  85.     {  202, 9.806650E+00,  200, "g"        }, /* *standard free-fall */
  86.     {  203, 2.540000E-02,  200, "in/s2"     }, /* *in/s2 */
  87.     /* ANGLE: */
  88.     {  300, 1.000000E+00,  300, "rad"        }, /* *rad */
  89.     {  301, 1.745329E-02,  300, "deg"        }, /* deg */
  90.     {  302, 2.908882E-04,  300, "min"        }, /* minute */
  91.     {  303, 4.848137E-06,  300, "sec"        }, /* second */
  92.     {  304, 1.570796E-02,  300, "grad"        }, /* grad */
  93.     /* AREA: */
  94.     {  400, 1.000000E+00,  400, "m2"        }, /* m2 */
  95.     {  401, 4.046873E+03,  400, "acre"        }, /* acre (US survey foot) */
  96.     {  402, 9.290304E-02,  400, "ft2"        }, /* *ft2 */
  97.     {  403, 1.000000E+04,  400, "ht"        }, /* hectare */
  98.     {  404, 6.451600E-04,  400, "in2"        }, /* *in2 */
  99.     {  405, 2.589998E+06,  400, "mi2"        }, /* US statute */
  100.     {  406, 8.361274E-01,  400, "yd2"        },
  101.     /* TORQUE: */
  102.     {  500, 1.000000E+00,  500, "N*m"        }, /* *(newton meter) */
  103.     {  501, 1.000000E-07,  500, "dyne*cm"    }, /* *dyne*cm */
  104.     {  502, 9.806650E+00,  500, "kgf*m"     }, /* *kgf*m */
  105.     {  503, 7.061552E-03,  500, "ozf*in"    },
  106.     {  504, 1.129848E-01,  500, "lbf*in"    },
  107.     {  505, 1.355818E+00,  500, "lbf*ft"    },
  108.     /* DENSITY: */
  109.     {  600, 1.000000E+00,  600, "kg/m3"     }, /* *kg/m3 */
  110.     {  601, 1.000000E+03,  600, "g/cm3"     }, /* *g/cm3 */
  111.     {  602, 7.489152E+00,  600, "ozm/gal"    }, /* US liquid */
  112.     {  603, 1.729994E+03,  600, "ozm/in3"    },
  113.     {  604, 1.601846E+01,  600, "lbm/ft3"    },
  114.     {  605, 2.767990E+04,  600, "lbm/in3"    },
  115.     {  606, 1.198264E+02,  600, "lbm/gal"    },
  116.     {  607, 5.153788E+02,  600, "slug/ft3"    },
  117.     /* ENERGY & WORK: */
  118.     {  700, 1.000000E+00,  700, "J"        }, /* *(joule) */
  119.     {  701, 1.055056E+03,  700, "Btu"        }, /* Btu (Inter. Table) */
  120.     {  702, 4.184000E+00,  700, "cal"        }, /* *(thermochemical) */
  121.     {  703, 1.602190E-19,  700, "ev"        }, /* electronvolt */
  122.     {  704, 1.000000E-07,  700, "erg"        }, /* *erg */
  123.     {  705, 1.355818E+00,  700, "ft*lbf"    },
  124.     {  706, 4.214011E-02,  700, "ft*poundal"    },
  125.     {  707, 3.600000E+06,  700, "kW*h"        },
  126.     {  708, 1.054804E+08,  700, "therm"     },
  127.     {  709, 4.184000E+09,  700, "ton-TNT"    }, /* nuclear equiv. TNT */
  128.     {  710, 3.600000E+03,  700, "W*h"        }, /* *W*h */
  129.     /* POWER PER UNIT AREA: */
  130.     {  800, 1.000000E+00,  800, "W/m2"        }, /* *W/m2 */
  131.     {  801, 1.135653E+04,  800, "Btu/ft2/s"    },
  132.     {  802, 3.154591E+00,  800, "Btu/ft2/h"    },
  133.     {  803, 4.184000E+04,  800, "cal/cm2/s"    }, /* *(thermochemical) */
  134.     {  804, 1.000000E+04,  800, "W/cm2"     }, /* *W/cm2 */
  135.     {  805, 1.550003E+03,  800, "W/in2"     },
  136.     /* FORCE: */
  137.     {  900, 1.000000E+00,  900, "N"        }, /* *(newton) */
  138.     {  901, 1.000000E-05,  900, "dyne"        }, /* *dyne */
  139.     {  902, 9.806650E+00,  900, "kgf"        }, /* kilogram-force */
  140.     {  903, 4.448222E+03,  900, "kip"        }, /* kip (1000 lbf) */
  141.     {  904, 2.780139E-01,  900, "ozf"        }, /* ounce-force */
  142.     {  905, 4.448222E+00,  900, "lbf"        }, /* lbf */
  143.     {  906, 1.382550E-01,  900, "poundal"    },
  144.     /* THERMAL CONDUCTIVITY: */
  145.     { 1000, 1.000000E+00, 1000, "W/m/K"     }, /* *W/m/K */
  146.     { 1001, 1.730735E+00, 1000, "Btu/h/ft/F"    },
  147.     { 1002, 1.442279E-01, 1000, "Btu*in/h/ft2/F"},
  148.     { 1003, 5.192204E+02, 1000, "Btu*in/s/ft2/F"},
  149.     /* HEAT CAPACITY: */
  150.     { 1100, 1.000000E+00, 1100, "J/kg/K"    }, /* *J/kg/K */
  151.     { 1101, 4.186800E+03, 1100, "Btu/lbm/F"    },
  152.     /* MASS: */
  153.     { 1200, 1.000000E+00, 1200, "kg"        }, /* *kg */
  154.     { 1201, 6.479891E-05, 1200, "gr"        }, /* *grain */
  155.     { 1202, 1.000000E-03, 1200, "gm"        }, /* gram */
  156.     { 1203, 9.806650E+00, 1200, "kgf*s2/m"    },
  157.     { 1204, 2.834952E-02, 1200, "ozm"        }, /* ounce-mass */
  158.     { 1205, 4.535924E-01, 1200, "lbm"        }, /* pound-mass */
  159.     { 1206, 1.459390E+01, 1200, "slug"        },
  160.     /* MASS PER UNIT LENGTH: */
  161.     { 1300, 1.000000E+00, 1300, "kg/m"        }, /* *kg/m */
  162.     { 1301, 1.111111E-07, 1300, "denier"    },
  163.     { 1302, 1.488164E+00, 1300, "lbm/ft"    },
  164.     { 1303, 1.785797E+01, 1300, "lbm/in"    },
  165.     { 1304, 1.000000E-06, 1300, "tex"        }, /* *tex */
  166.     /* MASS PER UNIT TIME: */
  167.     { 1400, 1.000000E+00, 1400, "kg/s"        }, /* *kg/s */
  168.     { 1401, 1.259979E-04, 1400, "lbm/h"     },
  169.     { 1402, 7.559873E-03, 1400, "lbm/min"    },
  170.     { 1403, 4.535924E-01, 1400, "lbm/s"     },
  171.     /* POWER: */
  172.     { 1500, 1.000000E+00, 1500, "W"        }, /* *(watt) */
  173.     { 1501, 2.930711E-01, 1500, "Btu/h"     },
  174.     { 1502, 1.055056E+03, 1500, "Btu/s"     },
  175.     { 1503, 4.184000E+00, 1500, "cal/s"     },
  176.     { 1504, 1.000000E+00, 1500, "erg/s"     }, /* *erg/s */
  177.     { 1505, 3.766161E-04, 1500, "ft*lbf/h"    },
  178.     { 1506, 2.25